package Entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Category implements Serializable {
	
	private String logo;

	private String name;

	private int id;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
	public Category(int id) {

		this.name = "Rubro " + id;
		this.setId(id);
		this.setLogo("http://cdn4.droidmill.com/media/market-media/com.health.rss_icon.png");
		
	}
	
	public Category(JSONObject obj) {
		try {
			this.name = obj.getString("name");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			this.logo = obj.getString("icon");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
		try {
			this.setId(obj.getInt("id"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	public static ArrayList<Category> createMocks(){
		ArrayList<Category> items = new ArrayList<Category>();
		for (int i = 0; i < 8; i++) {
			Category p  = new Category(i);
			items.add(p);
		}
		return items;
	}
	
	public static ArrayList<Category> getFromJSON(JSONObject itemsJSON){
		ArrayList<Category> items = new ArrayList<Category>();

		JSONArray itemsArray;
		try {
			itemsArray = itemsJSON.getJSONArray("categories");
			for(int i=0;i<itemsArray.length();i++){			
				Category p = new Category(itemsArray.getJSONObject(i));				
				items.add(p);					
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return items;
	}


	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getLogo() {
		return logo;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}
}
